import { getUserList,addUser,getUserId,putUserState,putEditUser,deleteUser,putUserRole} from '@/api/users'
const state={
    //发送请求的参数
    pagination:{
        query:'',
        //当前页码	
        pagenum:1,
        //每页显示条数
        pagesize:2,
    },
    //响应后取到的pagination数据
    paginationData:{
        //总页数
        total:null,
        //当前用户列表
        users:[]
    },
    //当前行的值
    currentRow:{}


};
const getters={
    Get_MesState(state,getters){
       /*  let newArr=[]
        state.paginationData.users.forEach(element => {
            newArr.push(element.mg_state)
        });
        return newArr */
        return state.paginationData.users.map(item=>{
           return item.mg_state;
        })
    }
};
const mutations={
   //修改参数pagenum
   CHANGE_PAGENUM(state,playLoad){
        state.pagination.pagenum=playLoad;
   },
   //修改参数pagesize
   CHANGE_PAGESIZE(state,playLoad){
        state.pagination.pagesize=playLoad;
  },
  //修改参数pagesize
    CHANGE_QUERY(state,playLoad){
      state.pagination.query=playLoad;
  },
  //修改获取到的pagination的total数据
  CHANGE_TOTAL(state,playLoad){
    state.paginationData.total=playLoad;
  },
  //修改获取到的pagination的users数据
  CHANGE_USERS(state,playLoad){
    state.paginationData.users=playLoad;
  },
  //修改当前行
  CHANGE_CURRENTROW(state,playLoad){
    state.currentRow=playLoad;
  },
  //修改获取到的pagination的users数据
  ADD_USERS(state,playLoad){
    state.paginationData.users.push(playLoad);
  }


};
const actions={
    //异步获取用户列表
    Acs_getUserList(content,playload){
        return new Promise((resolve,reject)=>{
             content.commit('CHANGE_QUERY','')   
             getUserList({...content.state.pagination})
             .then(res=>{
                let {total,users}=res;
                //console.log(users);
                content.commit('CHANGE_TOTAL',total);
                content.commit('CHANGE_USERS',users);
                 resolve(res)
             })
             .catch(err=>{
                 reject(err) 
             })
        })     
    },
    //异步添加用户
    Acs_postAddUser(content,playload){
        return new Promise((resolve,reject)=>{
            addUser(playload)
            .then(res=>{
                resolve(res)
            })
            .catch(err=>{
                reject(err) 
            })
       })    
    },
    //异步查询用户
    Acs_getUserId(content,playload){
        return new Promise((resolve,reject)=>{
            getUserId(playload)
            .then(res=>{
                content.commit('CHANGE_USERS',res.users)
                resolve(res)
            })
            .catch(err=>reject(err))
        })    
    },
    //异步修改开关状态
    Acs_putUserState(content,playload){
        return new Promise((resolve,reject)=>{
            putUserState(playload)
            .then(res=>resolve(res))
            .catch(err=>reject(err))
        })    
    },
    //异步编辑用户
    Acs_putEditUser(content,playload){
        return new Promise((resolve,reject)=>{
            putEditUser(playload)
            .then(res=>resolve(res))
            .catch(err=>reject(err))
        })    
    },
     //异步删除用户
    Acs_deleteUser(content,playload){
        //console.log(content.state.currentRow);
        return new Promise((resolve,reject)=>{
            deleteUser(content.state.currentRow)
            .then(res=>{
                resolve(res)
            })
            .catch(err=>{
                reject(err) 
            })
        })     
    },
    //异步分配角色
    Acs_putUserRole(content,playload){
        return new Promise((resolve,reject)=>{
            let {id}=content.state.currentRow;
            let data={id,...playload}
            //console.log(data);
            putUserRole(data)
            .then(res=>resolve(res))
            .catch(err=>reject(err)) 
        })     
    }

};
export default {
    namespaced:true,
    state,
    getters,
    mutations,
    actions
}